package cn.rkylin.oms.item.adapter;

import cn.rkylin.apollo.common.util.HttpUtils;
import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.item.request.DelistingItemRequest;
import cn.rkylin.oms.item.request.ListingItemRequest;
import cn.rkylin.oms.system.shop.vo.ShopVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component("suningItemAdapter")
public class SuningItemAdapter extends ItemAdapter {

    @Value("${gateway}")
    private String gateway;

    @Autowired
    LogUtil logUtil;

    /**
     * 同步店铺库存
     *
     * @param shopId
     * @return
     * @throws Exception
     */
    @Override
    public String synchShopStock(String shopId) throws Exception {
        String requestUrl = String.format(gateway + "/microservice-suning/stock/synchStockSuningItem?shopId=%s", shopId);
        String resJson = HttpUtils.sendByGet(requestUrl, null);
        Map<String, Object> resultMap = JSON.parseObject(resJson, Map.class);
        // 记录日志
        logUtil.info(new DBLog(shopId, "synchStockSuningItem", "synchShopStock",
                "[" + requestUrl + "," + resultMap.get("msg") + "]"));
        String msg = resultMap.get("msg").toString();
        return msg;
    }

    /**
     * 同步商品库存
     *
     * @param shopId
     * @param ecItemId
     * @return
     * @throws Exception
     */
    @Override
    public String synchItemStock(String shopId, String ecItemId) throws Exception {
        String requestUrl = String.format(gateway + "/microservice-suning/stock/synchStockSuningItemByItem?shopId=%s&ecItemId=%s", shopId, ecItemId);
        String resJson = HttpUtils.sendByGet(requestUrl, null);
        Map<String, Object> resultMap = JSON.parseObject(resJson, Map.class);
        // 记录日志
        logUtil.info(new DBLog(ecItemId, "synchStockSuningItemByItem", "synchItemStock",
                "[" + requestUrl + "," + resultMap.get("msg") + "]"));
        String msg = resultMap.get("msg").toString();
        return msg;
    }

    /**
     * 同步sku库存
     *
     * @param shopId
     * @param ecSkuId
     * @return
     * @throws Exception
     */
    @Override
    public String synchSkuStock(String shopId, String ecSkuId) throws Exception {
        String requestUrl = String.format(gateway + "/microservice-suning/stock/synchStockSuningItemBySku?shopId=%s&ecSkuId=%s", shopId, ecSkuId);
        String resJson = HttpUtils.sendByGet(requestUrl, null);
        Map<String, Object> resultMap = JSON.parseObject(resJson, Map.class);
        // 记录日志
        logUtil.info(new DBLog(ecSkuId, "synchStockSuningItemBySku", "synchSkuStock",
                "[" + requestUrl + "," + resultMap.get("msg") + "]"));
        String msg = resultMap.get("msg").toString();
        return msg;
    }

    /**
     * 商品上架
     *
     * @param shopId
     * @param listingItemRequest
     * @return
     * @throws Exception
     */
    @Override
    public String listingItem(String shopId, ListingItemRequest listingItemRequest) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> validateMap = super.pushDataValidate(shopId);

        if (validateMap.get("success").equals("y")) {
            ShopVO shop = (ShopVO) validateMap.get("shop");
            String requestParams = JSON.toJSONString(listingItemRequest);
            String requestUrl = gateway + "/microservice-suning/item/listingSuningItem?shopId=" + shop.getShopId();
            String resJson = HttpUtils.sendByPost(requestUrl, requestParams);
            Map<String, String> resultMap = JSON.parseObject(resJson, Map.class);
            // 记录日志
            logUtil.info(new DBLog(listingItemRequest.getEcItemCode(), "listingSuningItem", "listingItem",
                    "[" + requestParams + "," + resultMap.get("msg") + "]"));
            if ("true".equals(resultMap.get("success"))) {
                returnMap.put("success", "y");
                returnMap.put("msg", "");
            } else {
                returnMap.put("success", "n");
                returnMap.put("msg", resultMap.get("msg"));
            }
            return JSON.toJSONString(returnMap);
        }
        return JSON.toJSONString(returnMap);
    }

    /**
     * 商品下架
     *
     * @param shopId
     * @param delistingItemRequest
     * @return
     * @throws Exception
     */
    @Override
    public String delistingItem(String shopId, DelistingItemRequest delistingItemRequest) throws Exception {
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> validateMap = super.pushDataValidate(shopId);
        if (validateMap.get("success").equals("y")) {

            ShopVO shop = (ShopVO) validateMap.get("shop");
            String requestParams = JSON.toJSONString(delistingItemRequest);
            String requestUrl = gateway + "/microservice-suning/item/delistingSuningItem?shopId=" + shop.getShopId();
            String resJson = HttpUtils.sendByPost(requestUrl, requestParams);
            Map<String, String> resultMap = JSON.parseObject(resJson, Map.class);
            // 记录日志
            logUtil.info(new DBLog(delistingItemRequest.getEcItemCode(), "delistingSuningItem", "delistingItem",
                    "[" + requestParams + "," + resultMap.get("msg") + "]"));
            if ("true".equals(resultMap.get("success"))) {
                returnMap.put("success", "y");
                returnMap.put("msg", "");
            } else {
                returnMap.put("success", "n");
                returnMap.put("msg", resultMap.get("msg"));
            }
            return JSON.toJSONString(returnMap);
        }
        return JSON.toJSONString(returnMap);
    }
}
